package com.hibase.quartz.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.hibase.common.entity.ResponseModel;
import com.hibase.common.util.EntityUtil;
import com.hibase.core.web.annotation.HibasePostRequestMapping;
import com.hibase.core.web.entity.OperationIdEntity;
import com.hibase.core.web.entity.OperationIdsEntity;
import com.hibase.quartz.constant.QuartzType;
import com.hibase.quartz.entity.MdsJob;
import com.hibase.quartz.entity.MdsRedisJob;
import com.hibase.quartz.entity.vo.*;
import com.hibase.quartz.service.MdsJobService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author LYL
 * @since 2020-11-18
 */
@RestController
@RequestMapping("/mds-job")
@Api(tags = "定时任务")
@Slf4j
public class MdsJobController {

    @Autowired
    private MdsJobService mdsJobService;

    @Autowired
    private RedisTemplate<String,String> stringRedisTemplate;



    @ApiOperation(value = "查询列表", httpMethod = "POST")
    @HibasePostRequestMapping(value = "selectMdsJobList")
    public ResponseModel<MdsJobListVO> selectMdsJobList(@RequestBody MdsJobSearchVO searchVO) {

        PageInfo result = mdsJobService.selectMdsJobList(searchVO);

        result.setList(EntityUtil.transform(result.getList(), MdsJobListVO.class));

        return ResponseModel.successMsg(result);
    }

    @ApiOperation(value = "新增记录", httpMethod = "POST")
    @HibasePostRequestMapping(value = "insertMdsJob")
    public ResponseModel<String> insertMdsJob(@RequestBody MdsJobInsertVO insertVO) {

        MdsJob entity = EntityUtil.transform(insertVO, MdsJob.class);

        MdsRedisJob redisJob = new MdsRedisJob(){{

            setType(QuartzType.SAVEJOB.getKey());

            setMdsJob(mdsJobService.saveJobEntity(entity));

        }};

        stringRedisTemplate.convertAndSend("SingleClusterQuartz",JSON.toJSONString(redisJob));
//        RTopic  topic = redissonClient.getTopic("SingleClusterQuartz");
//        topic.publish(JSON.toJSONString(redisJob));

        return ResponseModel.successMsg(entity.getId());
    }

    @ApiOperation(value = "修改记录", httpMethod = "POST")
    @HibasePostRequestMapping(value = "updateMdsJob")
    public ResponseModel<String> updateMdsJob(@RequestBody MdsJobUpdateVO updateVO) {

        MdsJob entity = EntityUtil.transform(updateVO, MdsJob.class);

        MdsRedisJob redisJob = new MdsRedisJob(){{

            setType(QuartzType.SAVEJOB.getKey());

            setMdsJob(mdsJobService.saveJobEntity(entity));

        }};

        stringRedisTemplate.convertAndSend("SingleClusterQuartz",JSON.toJSONString(redisJob));
//        RTopic topic = redissonClient.getTopic("SingleClusterQuartz");
//        topic.publish(JSON.toJSONString(redisJob));
        return ResponseModel.successMsg(entity.getId());
    }

    @ApiOperation(value = "查询详情", httpMethod = "POST")
    @HibasePostRequestMapping(value = "viewMdsJobDetails")
    public ResponseModel<MdsJobViewVO> viewMdsJobDetails(@RequestBody OperationIdEntity operationidEntity) {

        return ResponseModel.successMsg(EntityUtil.transform(mdsJobService.viewMdsJob(operationidEntity.getId()), MdsJobViewVO.class));
    }

    @ApiOperation(value = "删除记录", httpMethod = "POST")
    @HibasePostRequestMapping(value = "/deleteMdsJob")
    public ResponseModel<String> deleteMdsJob(@RequestBody OperationIdsEntity operationIdsEntity) {

//        MdsRedisJob redisJob = new MdsRedisJob(){{
//
//            setType(QuartzType.DELETJOB.getKey());
//
//            setMdsJob(new MdsJob(){{setId(delete.getId());}});
//
//        }};
//        stringRedisTemplate.convertAndSend("SingleClusterQuartz",JSON.toJSONString(redisJob));
//        RTopic topic = redissonClient.getTopic("SingleClusterQuartz");
//        topic.publish(JSON.toJSONString(redisJob));
        mdsJobService.deleteMdsJob(operationIdsEntity.getIds());
        return ResponseModel.successMsg();
    }

    @ApiOperation(value = "重启所有定时任务", httpMethod = "POST")
    @HibasePostRequestMapping(value = "/reStartAllJobs")
    public ResponseModel<String> reStartAllJobs() {

        MdsRedisJob redisJob = new MdsRedisJob(){{

            setType(QuartzType.RESTARTALLJOB.getKey());

            setMdsJob(null);

        }};

        stringRedisTemplate.convertAndSend("SingleClusterQuartz",JSON.toJSONString(redisJob));
//        RTopic topic = redissonClient.getTopic("SingleClusterQuartz");
//        topic.publishAsync(JSON.toJSONString(redisJob));
        return ResponseModel.successMsg();

    }

    @ApiOperation(value = "立即执行任务", httpMethod = "POST")
    @HibasePostRequestMapping(value = "immediateJob")
    public ResponseModel<MdsJobViewVO> immediateJob(@RequestBody MdsImmediateJob mdsImmediateJob) {

        mdsJobService.immediateJob(mdsImmediateJob);

        return ResponseModel.successMsg();
    }



//    @PostConstruct
//    public void MdsJobInitialize() {
//
//
//    }

}
